//
//  VisterView.swift
//  WeiBo2017
//
//  Created by 王璐 on 2017/4/13.
//  Copyright © 2017年 wl. All rights reserved.
//

import UIKit
import SnapKit

/// VistorViewDelegate
protocol VistorViewDelegate : NSObjectProtocol {
    /// 访客视图注册
    func vistorViewDidRegister()
    /// 访客视图登录
    func vistorViewDidLogin()
    
}

class VisterView: UIView {
    
    //MARK: - 代理属性(代理一定要加weak)
    weak var delegate : VistorViewDelegate?
    
    //MARK: - 构造函数
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
        
        //添加监听方法
        registerButton.addTarget(self, action: #selector(VisterView.registerButtonClick), for: .touchUpInside)
        loginButton.addTarget(self, action: #selector(VisterView.loginButtonClick), for: .touchUpInside)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 登录注册按钮的监听方法
    @objc fileprivate func registerButtonClick(){
//        print("点击注册按钮")
        delegate?.vistorViewDidRegister()
        
    }
    @objc fileprivate func loginButtonClick(){
        //print("点击登录按钮")
        delegate?.vistorViewDidLogin()
    }
    
    
    //MARK: - 公共方法
    /// 设置访客视图信息
    ///
    /// - parameter imageName: 图像名,首页默认 为nil
    /// - parameter message:   消息标签名
    func  setUpInfo(imageName:String?=nil,message:String){
        messageLabel.text = message
        guard let imageName = imageName else {
            startAnimation()
            return
        }
        homeIconView.isHidden = true
        maskIconView.isHidden = true
        iconView.image = UIImage(named:imageName)
    }
    
    /// 开始动画
    fileprivate func startAnimation(){
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.toValue = 2*M_PI
        ani.repeatCount = MAXFLOAT
        ani.duration = 20
        //对于需要循环播放的动画需要设置此属性
        //视图离开屏幕后 不会被销毁,当动画所在的父视图被销毁,动画会连带被销毁,不需要考虑释放问题
        ani.isRemovedOnCompletion = false
        iconView.layer.add(ani, forKey: nil)
        
    }
    
    
    //MARK: - 私有控件
    ///图片视图
    fileprivate lazy var iconView : UIImageView = UIImageView(wl_imageName: "visitordiscover_feed_image_smallicon")
    
    /// 遮罩视图
    fileprivate lazy var maskIconView : UIImageView = UIImageView(wl_imageName: "visitordiscover_feed_mask_smallicon")

    /// 小房子图标
    fileprivate lazy var homeIconView : UIImageView = UIImageView(wl_imageName: "visitordiscover_feed_image_house")
    
    /// 消息标签
    fileprivate lazy var messageLabel : UILabel = UILabel(
        wl_text: "关注一些人,回这里看看有什么惊喜关注一些人,回这里看看有什么惊喜",
        alignment: .center)
    
    /// 注册按钮
    fileprivate lazy var registerButton :UIButton = UIButton(
        title: "注册",
        color: UIColor.orange,
        backgroudImageName: "common_button_white_disable")
    
    /// 登录按钮
    fileprivate lazy var loginButton : UIButton = UIButton(
        title: "登录",
        backgroudImageName: "common_button_white_disable")

}

// MARK: - 设置界面
extension VisterView{
    
    fileprivate func setupUI(){
        
      //  backgroundColor = UIColor.green
        ///1. 添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //设置文字对齐
       // messageLabel.textAlignment = .center
        /// 2.自动布局
        //图片视图的布局
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        //小房子图片的布局
        homeIconView.snp.makeConstraints { (make) in
            make.center.equalTo(iconView)
        }
        //消息标签
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp.bottom).offset(12)
            make.size.equalTo(CGSize(width: 224, height: 36))
        }
        //注册按钮的布局
        registerButton.snp.makeConstraints { (make) in
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp.bottom).offset(12)
            make.size.equalTo(CGSize(width:100,height:36))
        }
        //登录按钮的布局
        loginButton.snp.makeConstraints { (make) in
            make.right.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        //遮罩视图的布局
        maskIconView.snp.makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(registerButton)
        }
        //空白区域的背景颜色
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)

    }
    
    
}
